﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;


public partial class Theme_3_MyMsg : PowerCreator.LMS.Web.Base.BasePage
{
    PowerCreator.LMS.ViewQuery.ViewQuery opViewQuery = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    PowerCreator.LMS.Web.DL.Project.TOUserMsg objMsg = new PowerCreator.LMS.Web.DL.Project.TOUserMsg();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    PowerCreator.LMS.DL.Project.Entity.VOTOLearner volearner = new PowerCreator.LMS.DL.Project.Entity.VOTOLearner();
    PowerCreator.LMS.Web.DL.Project.TOLearner objlearer = new PowerCreator.LMS.Web.DL.Project.TOLearner();
    PowerCreator.LMS.DL.Project.Entity.VOTOAdvancedUser voadvanceduser = new PowerCreator.LMS.DL.Project.Entity.VOTOAdvancedUser();
    PowerCreator.LMS.Web.DL.Project.TOAdvancedUser objadvanceduser = new PowerCreator.LMS.Web.DL.Project.TOAdvancedUser();
    private string NoreadedSql = "";
    private string ReadedSql = "";
    private string MyCreateSql = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = config.GetLanagerValueByKey("WebTitle", PowerCreator.LMS.Language.LanagetItemType.WebTitle); //"PowerCreator 学习管理系统";
        if (!IsPostBack)
        {
            //BinderNews();
            CheckUser();
            GetNewSearch();
            BinderNoReadedRPT();
            BinderReadedRPT();
            BinderMyCreateRPT();
            BinderTeacher();
            BinderClasss();
            BinderStudent();
        }
    }

    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            PowerCreator.LMS.Common.Script.Script.ShowAndCloseMe(this, config.GetLanagerValueByKey("theme_Channel_LoginMsg", PowerCreator.LMS.Language.LanagetItemType.Page));
            return;
        }
    }
    protected override void InitPage()
    {
        base.InitPage();
    }

    private void GetNewSearch()
    {
        PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            NoreadedSql = " Maek=0 and (Type=1 or Type=3) and  Reciver=" + login.UserID;
            ReadedSql = " Maek=1 and (Type=1 or Type=3) and Reciver=" + login.UserID;
            MyCreateSql = "(Type=3 or Type=4) and Creator=" + login.UserID;
        }
    }

    private void BinderNoReadedRPT()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            td_MyNoReadedNew.Visible = false;
            return;
        }
        DataTable dt = new DataTable();
        string orderby = " Type asc,UserMsg_Id desc";
        int iPageSize = objMsg.Count(NoreadedSql);
        lbl_NoRead.Text = "&nbsp;("+iPageSize.ToString()+")条";
        dt = objMsg.Query(NoreadedSql, orderby, iPageSize, 1);
        dt.Columns.Add("Name");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["Type"].ToString() == "1")
            {
                voadvanceduser = objadvanceduser.Load(int.Parse(dt.Rows[i]["Creator"].ToString()));
                dt.Rows[i]["Name"] = voadvanceduser.Name;
            }
            else if (dt.Rows[i]["Type"].ToString() == "3")
            {
                volearner = objlearer.Load(int.Parse(dt.Rows[i]["Creator"].ToString()));
                dt.Rows[i]["Name"] = volearner.Name;
            }
        }
        rpt_NoReadedNew.DataSource = dt;
        rpt_NoReadedNew.DataBind();
    }
    private void BinderReadedRPT()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            td_MyNoReadedNew.Visible = false;
            return;
        }
        DataTable dt = new DataTable();
        string orderby = " Type asc,UserMsg_Id desc";
        int iPageSize = objMsg.Count(ReadedSql);
        lbl_Read.Text = "&nbsp;("+iPageSize.ToString()+")条";
        dt = objMsg.Query(ReadedSql, orderby, iPageSize, 1);
        dt.Columns.Add("Name");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["Type"].ToString() == "1")
            {
                voadvanceduser = objadvanceduser.Load(int.Parse(dt.Rows[i]["Creator"].ToString()));
                dt.Rows[i]["Name"] = voadvanceduser.Name;
            }
            else if (dt.Rows[i]["Type"].ToString() == "3")
            {
                volearner = objlearer.Load(int.Parse(dt.Rows[i]["Creator"].ToString()));
                dt.Rows[i]["Name"] = volearner.Name;
            }
        }
        rpt_ReadedNew.DataSource = dt;
        rpt_ReadedNew.DataBind();
    }
    private void BinderMyCreateRPT()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            td_MyCreateNew.Visible = false;
            return;
        }
        DataTable dt = new DataTable();
        string orderby = "Type asc, UserMsg_Id desc";
        int iPageSize = objMsg.Count(MyCreateSql);
        lbl_MyCreate.Text = "&nbsp;("+iPageSize.ToString()+")条";
        dt = objMsg.Query(MyCreateSql, orderby, iPageSize, 1);
        dt.Columns.Add("Name");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["Type"].ToString() == "4")
            {
                voadvanceduser = objadvanceduser.Load(int.Parse(dt.Rows[i]["Reciver"].ToString()));
                dt.Rows[i]["Name"] = voadvanceduser.Name;
            }
            else if (dt.Rows[i]["Type"].ToString() == "3")
            {
                volearner = objlearer.Load(int.Parse(dt.Rows[i]["Reciver"].ToString()));
                dt.Rows[i]["Name"] = volearner.Name;
            }
        }

        rpt_MyCreateNew.DataSource = dt;
        rpt_MyCreateNew.DataBind();
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "ldel")
        {
            if (objMsg.Del(int.Parse(e.CommandArgument.ToString())))
            {
                Script.ReloadMe();
            }

        }

    }
    protected void rpt2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }
    protected void rptNew2_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "ldel")
        {
            if (objMsg.Del(int.Parse(e.CommandArgument.ToString())))
            {
                Script.ReloadMe();
            }

        }

    }
    protected void rpt3_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }
    protected void rptNew3_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "ldel")
        {
            if (objMsg.Del(int.Parse(e.CommandArgument.ToString())))
            {
                Script.ReloadMe();
            }

        }

    }
    protected void rdbtn_student_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtn_student.Checked)
        {
            tr_teacher.Visible= false;
            tr_student.Visible = true;
        }
        else if (rbtn_teacher.Checked)
        {
            tr_teacher.Visible = true;
            tr_student.Visible = false;
        }
    }
    protected void rdbtn_teacher_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtn_teacher.Checked)
        {
            tr_teacher.Visible = true;
            tr_student.Visible = false;
        }
        else if (rbtn_student.Checked)
        {
            tr_teacher.Visible = false;
            tr_student.Visible = true;
        }
    }

    private void BinderTeacher()
    {
        PowerCreator.LMS.Web.DL.Project.TOAdvancedUser obj=new PowerCreator.LMS.Web.DL.Project.TOAdvancedUser();
        string sql = "";
        DataTable dt = new DataTable();
        dt = obj.Query(sql);
        ddl_teacher.DataSource = dt;
        ddl_teacher.DataTextField = "Name";
        ddl_teacher.DataValueField = "AUser_Id";
        ddl_teacher.DataBind();
    }
    private void BinderClasss()
    {
        PowerCreator.LMS.Web.DL.Project.TOClass obj = new PowerCreator.LMS.Web.DL.Project.TOClass();
        string sql = "";
        DataTable dt = new DataTable();
        dt = obj.Query(sql);
        ddl_Class.DataSource = dt;
        ddl_Class.DataTextField = "Name";
        ddl_Class.DataValueField = "Class_Id";
        ddl_Class.DataBind();
    }
    protected void on_ddlClass_change(object sender, EventArgs e)
    {
        BinderStudent();
    }
    private void BinderStudent()
    {
        PowerCreator.LMS.Web.DL.Project.TOLearner obj = new PowerCreator.LMS.Web.DL.Project.TOLearner();
        string sql = " Learner_Id in (select UserID from TO_UserOfClass where ClassID="+ddl_Class.SelectedValue.ToString()+")";
        DataTable dt = new DataTable();
        dt = obj.Query(sql);
        ddl_Student.DataSource = dt;
        ddl_Student.DataTextField = "Name";
        ddl_Student.DataValueField = "Learner_Id";
        ddl_Student.DataBind();
    }
    private bool CheckPageData()
    {
        lbl_msg_Error.Text = "";
        if (string.IsNullOrEmpty(txt_Title.Text))
        {
            lbl_msg_Error.Text ="消息标题不能为空，请输入！";
            txt_Title.Focus();
            return false;
        }

        if (string.IsNullOrEmpty(txt_Content.Text))
        {
            lbl_msg_Error.Text = lbl_msg_Error.Text+"<br />消息内容不能为空，请输入！";
            txt_Content.Focus();
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        PowerCreator.LMS.DL.Project.Entity.VOTOUserMsg vo = new PowerCreator.LMS.DL.Project.Entity.VOTOUserMsg();
        PowerCreator.LMS.Web.DL.Project.TOUserMsg obj = new PowerCreator.LMS.Web.DL.Project.TOUserMsg();
        PowerCreator.LMS.Web.DL.User.LoginEntity login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            vo.Creator = login.UserID;
            vo.Maek = false;
            if (rbtn_teacher.Checked)
            {
                vo.Reciver = int.Parse(ddl_teacher.SelectedValue);
                vo.Type = 4;
            }
            else if (rbtn_student.Checked)
            {
                vo.Reciver = int.Parse(ddl_Student.SelectedValue);
                vo.Type = 3;
            }
            vo.Title = txt_Title.Text.Trim();
            vo.Content = txt_Content.Text.Trim();
            vo.CreateDate = DateTime.Now;
            if(obj.Add(vo))
            {
                string url = "MyMsg.aspx";
                Script.ShowAndRedirect(this, "发送成功！", url);
            }
        }
    }
}